/*
 * SarsPacketMessages1.cs
 * Definiton of SARS Packet content
 * objects which also generate the payload.
 */


using Jayrock.Json;
using System;
using System.Net;
using System.IO;
using System.Text;
using System.Collections.Generic;


namespace LibSars
{

	// Pseudo packet for unknown / invalid SARS messages (NULL object pattern).
	public class SarsPacketMessageURPSEUDO : SarsPacketMessage{
		public override byte[] GeneratePayloadBytes(){
			return new byte[0];
		}
		public override void FromBytes(ref byte[] bytes){}
		public override UInt16 GetMTypeInt(){ return 0x0000; }
		private SarsPacketMessageURPSEUDO(){}
		static SarsPacketMessage instance = new SarsPacketMessageURPSEUDO();
		public static SarsPacketMessage Instance { get { return instance; } }
	}

	// Handshake packet.
	public class SarsPacketMessageIMINURCHAT : SarsPacketMessage{
		public override byte[] GeneratePayloadBytes(){
			List<byte> byteList = new List<byte>();
			return byteList.ToArray();
		}
		public override void FromBytes(ref byte[] bytes){}
		public const UInt16 MTypeInt = 0x0001;
		public override UInt16 GetMTypeInt(){ return MTypeInt; }
	}
	
	// Text send packet.
	public class SarsPacketMessageSAITHIS : SarsPacketMessage{
		public override byte[] GeneratePayloadBytes(){
			return System.Text.Encoding.ASCII.GetBytes(Text);
		}
		public SarsPacketMessageSAITHIS(){}
		public SarsPacketMessageSAITHIS(string daText){ Text = daText; }
		public string Text = "";
		public override void FromBytes(ref byte[] bytes){
			Text = System.Text.Encoding.ASCII.GetString(bytes);
		}
		public const UInt16 MTypeInt = 0x0004;
		public override UInt16 GetMTypeInt(){ return MTypeInt; }
	}

	public struct SarsPeerItem {
		public UInt16 Id;
		public UInt16 AddedBy;
		public IPAddress IP;
		
		public SarsPeerItem(UInt16 p_Id, UInt16 p_AddedBy, IPAddress p_IP){
			Id = p_Id;
			AddedBy = p_AddedBy;
			IP = p_IP;
		}

		/// <summary>
		/// Serialize Peer as string
		/// </summary>
		/// <returns>Single Peer as string.</returns>
		public string GetSerialized(){
			// Open memory stream
			System.IO.MemoryStream memStream = new System.IO.MemoryStream();
			// Initialize TextWriter and JsonWriter for serialization
			System.IO.TextWriter twriter = new System.IO.StreamWriter(memStream);
			twriter.Write(Id.ToString()+"\t");
			twriter.Write(AddedBy.ToString()+"\t");     
	        twriter.WriteLine(IP.ToString());     
	        twriter.Flush();
	        // Save serialized
	       	string serialized = System.Text.Encoding.ASCII.GetString(memStream.ToArray());
	       	twriter.Close();
	        memStream.Dispose();
	        
	        return serialized;
		}

		public void DeserializeFrom(string text){
			string[] split = text.Split("\t".ToCharArray());
			Id = Convert.ToUInt16(split[0]);
			AddedBy = Convert.ToUInt16(split[1]);
			IP = IPAddress.Parse(split[2]);
		}
		
	}

	// Text send packet.
	public class SarsPacketMessageUHASCL : SarsPacketMessage{
		
		public override byte[] GeneratePayloadBytes(){

			StringBuilder tmpText = new StringBuilder();

		
			if (PeerList != null)
				foreach(SarsPeerItem item in PeerList){
					tmpText.Append(item.GetSerialized());
				}
			
			return System.Text.Encoding.ASCII.GetBytes(tmpText.ToString());
		}

		public SarsPeerItem[] PeerList = null;
		
		public SarsPacketMessageUHASCL(){}
		
		public override void FromBytes(ref byte[] bytes){
			PeerList = null;
			MemoryStream memStream = new MemoryStream(bytes);
			TextReader treader = new StreamReader(memStream);
			
			List<SarsPeerItem> items = new List<SarsPeerItem>();
			string line = null;
			while( (line = treader.ReadLine()) != null){
				SarsPeerItem item = new SarsPeerItem();
				item.DeserializeFrom(line);
				items.Add(item);
			}
			
			PeerList = items.ToArray();
			
				          
			
		}
		public const UInt16 MTypeInt = 0x0002;
		public override UInt16 GetMTypeInt(){ return MTypeInt; }
	}

}